// pages/ucenter/my-income/my-income.js
import * as API_Distribution from '../../api/distribution.js'
import { Foundation } from '../../ui-utils/index.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    from:'',//页面来源
    finished: false,
    showGoTop: false,
    scrollTop: 0,//滚动高度
    scrollHeight: '',
    pageCount: 0,
    params: {
	  start_time: null,
	  end_time: null,
      page_no: 1,
      page_size: 20,
      type: 1
    },
    moneyList:[],
    pageCount: 0,
    finished: false, 
    msg:'',
    //类型
    type:'',
    moneyTotal: {
      expenditure: "0.00",
      income: "0.00"
    },
    //用户创建时间
    creat_time:'',
    //获取当前时间
    now_time:"",
	// 选择的起始结束时间
	timeStart: '',
	timeEnd: '',
	// 测试 是否成功
	isPickerRender: false,
	isPickerShow: false,
	startTime: "2019-01-01 12:32:44",
	endTime: "2019-12-01 12:32:44",
	pickerConfig: {
	  endDate: true,
	  column: "second",
	  dateLimit: true,
	  initStartTime: "2019-01-01 12:32:44",
	  initEndTime: "2019-12-01 12:32:44",
	  limitStartTime: "2015-05-06 12:32:44",
	  limitEndTime: "2055-05-06 12:32:44"
	}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const user = wx.getStorageSync('user');
    let time = Foundation.unixToDate(user.create_time,'yyyy-MM-dd')
    this.setData({creat_time:time});
    let Data = new Date();
    let year = Data.getFullYear();
    let month = Data.getMonth()+1>9?(Data.getMonth()+1):'0'+(Data.getMonth()+1);
    let day = Data.getDate()>9?(Data.getDate()):'0'+(Data.getDate());
    this.setData({now_time:year+'-'+month+'-'+day})
	this.setData({
		startTime: this.data.creat_time,
		endTime: this.data.now_time
	})
	
	this.setData({
		'params.start_time': new Date(this.data.creat_time).getTime(),
		'params.end_time': new Date(this.data.now_time).getTime() 
	})
	this.setData({
		'pickerConfig.initStartTime': this.data.creat_time,
		'pickerConfig.initEndTime': this.data.now_time,
		'pickerConfig.limitStartTime': this.data.creat_time,
		'pickerConfig.limitEndTime': this.data.now_time
	})
    this.setData({
      from: options.from || ''
    })
    this.setData({type:options.type});
    if(options.type=='come'){
      this.setData({'params.type':1});
      this.GET_MoneyList();
      
    }else{
      this.setData({'params.type':0});
      this.GET_MoneyList();
    }
    API_Distribution.getMoneySum().then(data => {
      data.expenditure = Foundation.formatPrice(data.expenditure);
      data.income = Foundation.formatPrice(data.income);
      this.setData({moneyTotal:data})
    });
    
  },
  // 测试是否成功 S
  pickerShow: function() {
      this.setData({
        isPickerShow: true,
        isPickerRender: true,
        chartHide: true
      });
    },
    pickerHide: function() {
      this.setData({
        isPickerShow: false,
        chartHide: false
      });
    },
  
    bindPickerChange: function(e) {
      console.log("picker发送选择改变，携带值为", e.detail.value);
      // console.log(this.data.sensorList);
  
      this.getData(this.data.sensorList[e.detail.value].id);
      // let startDate = util.formatTime(new Date(new Date().getTime() - 24 * 60 * 60 * 1000 * 7));
      // let endDate = util.formatTime(new Date());
      this.setData({
        index: e.detail.value,
        sensorId: this.data.sensorList[e.detail.value].id
        // startDate,
        // endDate
      });
    },
    setPickerTime: function(val) {
      let data = val.detail;
      this.setData({
        startTime: data.startTime,
        endTime: data.endTime
      });
	  var startTime = ''
	  var endTime = ''
	  startTime = data.startTime ? data.startTime.slice(0, 10) : this.data.creat_time
	  endTime = data.endTime ? data.endTime.slice(0, 10) : this.data.now_time,
	  console.log(startTime)
	  console.log(endTime)
	  this.setData({
		'params.start_time': new Date(startTime).getTime(),
		'params.end_time': new Date(endTime).getTime()
	  })
	  this.setData({ moneyList: []})
	  this.GET_MoneyList()
    },
  // 测试是否成功 E
  
  GET_MoneyList(){
    API_Distribution.getMoneyList(this.data.params).then(response=>{
      let _pageCount = Math.ceil(response.data_total / this.data.params.page_size)
      this.setData({ pageCount: _pageCount })
      const data = response.data
      if(data && data.length){
        data.forEach(key=>{
          key.build_time = Foundation.unixToDate(key.build_time)
        })
        this.setData({ moneyList: this.data.moneyList.concat(data)})
      }else{
        this.setData({ finished: true })
      }
    })
    this.setData({
      scrollHeight: wx.getSystemInfoSync().windowHeight - 110
    })
  },
  loadMore: function () {
    if (!this.data.finshed) {
      this.setData({ "params.page_no": this.data.params.page_no + 1 })
      if (this.data.pageCount >= this.data.params.page_no) {
        this.GET_MoneyList()
      } else {
        this.setData({ msg: '已经到底了~' })
      }
    }
  },
  scroll: function (e) {
    let that = this
    if (e.detail.scrollTop > 200) {
      that.setData({showGoTop: true})
    } else {
      that.setData({showGoTop: false})
    }
  },
  //返回顶部
  goTop: function () { this.setData({ scrollTop: 0 })},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})